/**
 * JSparrow - Une version numérique du jeu "Attention Pirates !" - Copyright (C) 2014 Black Pearl
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see <http://www.gnu.org/licenses/>.
 */
package App.Vue;

import App.Controleur.Jeu;
import static App.Vue.JPanel_Principal.TRANSPARENT;
import App.main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Panneau_ChargementPartie extends JPanel_Principal {

    private boolean flagInit = true;
    private Image table;
    private final ArrayList<String> listParties;
    private final JComboBox combo = new JComboBox();
    private final JButton validerBouton;
    private final JButton retourBouton;
    private final JLabel label;
    private final JPanel panelInterieur;
    private final GridBagConstraints gbc;
    protected Panneau_CreationPartie partie;
    protected Jeu controleur;
    protected Fenetre_Principale fenetre;
    protected Hashtable dataParties;

    @SuppressWarnings("unchecked")
    public Panneau_ChargementPartie(ArrayList<String> parties, Jeu controleur, Fenetre_Principale fenetre) {
        this.listParties = parties;
        this.controleur = controleur;
        this.fenetre = fenetre;
        combo.setPreferredSize(new Dimension(100, 20));
        if (listParties.size() > 0) {
            for (String partie : listParties) {
                String[] tableauPartie = partie.split("-");
                this.dataParties = new Hashtable<>();
                for (int i = 0; i < tableauPartie.length; i++) {
                    if (i % 2 == 0) {
                        this.dataParties.put(tableauPartie[i], tableauPartie[i + 1]);
                        combo.addItem(tableauPartie[i]);
                    }
                }
            }
        }
        this.panelInterieur = new JPanel();
        this.validerBouton = new JButton("Valider");
        this.retourBouton = new JButton("Retour");
        if (listParties.size() > 0) {
            this.label = new JLabel("Liste des parties");
        } else {
            this.label = new JLabel("Pas de parties");

        }
        validerBouton.setFont(boutonFont);
        retourBouton.setFont(boutonFont);
        label.setFont(boutonFont);
        label.setForeground(Color.WHITE);
        validerBouton.addActionListener(new Panneau_ChargementPartie.boutonListener("Valider"));
        retourBouton.addActionListener(new Panneau_ChargementPartie.boutonListener("Retour"));
        this.gbc = new GridBagConstraints();
        this.setLayout(new GridBagLayout());
        this.panelInterieur.setLayout(new GridLayout(3, 2, 10, 10));
        this.panelInterieur.setBorder(new EmptyBorder(50, 50, 20, 50));
        // Réglage de la transparence des fonds et des bordures
        this.setBackground(TRANSPARENT);
        this.setOpaque(false);
        this.panelInterieur.setOpaque(false);
        this.panelInterieur.setBackground(TRANSPARENT);
        combo.setOpaque(false);
        combo.setBorder(javax.swing.BorderFactory
                .createEmptyBorder());
        this.panelInterieur.add(label);
        if (listParties.size() > 0) {
            this.panelInterieur.add(combo);
            this.panelInterieur.add(validerBouton);
        }
        this.panelInterieur.add(retourBouton);
        // Centrer le panel intérieur
        gbc.gridy = 1;
        gbc.gridx = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        this.add(panelInterieur, gbc);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (flagInit) {
            Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
            try {
                BufferedImage bufferedFond = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/backgroundTable.jpg"));
                table = bufferedFond.getScaledInstance(screenBounds.width, screenBounds.height, 0);
            } catch (IOException e) {
                e.printStackTrace();
            }
            flagInit = false;
        }
        g.drawImage(table, 0, 0, this);
    }

    class boutonListener implements ActionListener {

        String bouton;

        public boutonListener(String bouton) {
            super();
            this.bouton = bouton;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            switch (bouton) {
                case "Valider":
                    String nomPartie = (String) combo.getSelectedItem();
                    int nombreJoueurs = -1;
                    Enumeration data = dataParties.keys();
                    while (data.hasMoreElements()) {
                        String key = (String) data.nextElement();
                        if (key.equals(nomPartie)) {
                            nombreJoueurs = Integer.parseInt((String) dataParties.get(key));
                        }
                    }
                    if (nombreJoueurs != -1) {
                        fenetre.chargerJeu(nombreJoueurs);
                        controleur.chargerPartie(nomPartie);
                    } else {
                        if (main.DEBUG_MODE) {
                            System.out.println("Impossible de charger la partie.");
                        }
                    }
                    break;
                case "Retour":
                    // @TODO revenir sur le menu principal
                    break;
                default:
            }
        }
    }

}
